package jnpf.database.enums.datatype;

import jnpf.database.constant.ViewDataTypeConst;
import jnpf.database.model.DataTypeModel;
import jnpf.database.source.DbBase;
import jnpf.util.StringUtil;

/**
 * 字段类型枚举
 *
 * @author JNPF开发平台组 YanYu
 * @version V3.2.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/6/29
 */
public enum DataTypeEnum {

    /**
     * 如{主类型},{次类型}:({默认字符长度},{限制长度}(*:不允许设置))
     */

    /**
     * 字符
     */
    VARCHAR(
            ViewDataTypeConst.VARCHAR,
            DtMysqlEnum.VARCHAR,
            DtOracleEnum.VARCHAR2,
            DtSqlserverEnum.VARCHAR,
            DtDmEnum.VARCHAR,
            DtKingbaseEnum.VARCHAR,
            DtPostgreEnum.VARCHAR
    ),
    /**
     * 日期时间
     * 日期统一不指定长度
     */
    DATE_TIME(
            ViewDataTypeConst.DATE_TIME,
            DtMysqlEnum.DATA_TIME,
            DtOracleEnum.DATA,
            DtSqlserverEnum.DATA_TIME,
            DtDmEnum.DATA_TIME,
            DtKingbaseEnum.TIME_STAMP,
            DtPostgreEnum.TIME_STAMP
    ),
    /**
     * 浮点
     */
    DECIMAL(
            ViewDataTypeConst.DECIMAL,
            DtMysqlEnum.DECIMAL,
            DtOracleEnum.NUMBER_DECIMAL,
            DtSqlserverEnum.DECIMAL,
            DtDmEnum.DECIMAL,
            DtKingbaseEnum.NUMERIC,
            DtPostgreEnum.NUMERIC
    ),
    /**
     * 文本
     */
    TEXT(
            ViewDataTypeConst.TEXT,
            DtMysqlEnum.TEXT,
            DtOracleEnum.CLOB,
            DtSqlserverEnum.TEXT,
            DtDmEnum.TEXT,
            DtKingbaseEnum.TEXT,
            DtPostgreEnum.TEXT
    ),
    /**
     * 整型
     * SqlServer、PostGre:int不能指定长度
     */
    INT (
            ViewDataTypeConst.INT,
            DtMysqlEnum.INT,
            DtOracleEnum.NUMBER_INT,
            DtSqlserverEnum.INT,
            DtDmEnum.INT,
            DtKingbaseEnum.INTEGER,
            DtPostgreEnum.INT4
    ),
    /**
     * 长整型
     */
    BIGINT(
            ViewDataTypeConst.BIGINT,
            DtMysqlEnum.BIGINT,
            DtOracleEnum.NUMBER_BIG,
            DtSqlserverEnum.BIGINT,
            DtDmEnum.BIGINT,
            DtKingbaseEnum.BIGINT,
            DtPostgreEnum.INT8
    ),
    /**
     * oracle数字类型
     */
    ORACLE_NUMBER(
            ViewDataTypeConst.ORACLE_NUMBER,
            null,
            DtOracleEnum.NUMBER_VIEW,
            null,
            null,
            null,
            null
    );

    private final String viewFieldType;

    private final DtMysqlEnum dtMysqlEnum;

    private final DtOracleEnum dtOracleEnum;

    private final DtSqlserverEnum dtSqlserverEnum;

    private final DtDmEnum dtDmEnum;

    private final DtKingbaseEnum dtKingbaseEnum;

    private final DtPostgreEnum dtPostgreEnum;

    DataTypeEnum(String viewFieldType,DtMysqlEnum dtMysqlEnum,DtOracleEnum dtOracleEnum,
                 DtSqlserverEnum dtSqlserverEnum, DtDmEnum dtDmEnum, DtKingbaseEnum dtKingbaseEnum,
                 DtPostgreEnum dtPostgreEnum){
        this.viewFieldType = viewFieldType;
        this.dtMysqlEnum = dtMysqlEnum;
        this.dtOracleEnum = dtOracleEnum;
        this.dtSqlserverEnum = dtSqlserverEnum;
        this.dtDmEnum = dtDmEnum;
        this.dtKingbaseEnum = dtKingbaseEnum;
        this.dtPostgreEnum = dtPostgreEnum;
    }

    /**
     * 获取数据库类型 通过view字段类型
     * @param viewFieldType view字段类型
     * @param db 数据基类
     * @return view类型对象的数据类型
     */
    public static DataTypeModel getDataTypeModel(String viewFieldType, DbBase db) throws Exception{
        if(StringUtil.isNotNull(viewFieldType)){
            for (DataTypeEnum value : DataTypeEnum.values()) {
                if(value.getViewFieldType().equalsIgnoreCase(viewFieldType)){
                    return db.getDataTypeModel(value);
                }
            }
        }
        return null;
    }

    public String getViewFieldType() {
        return viewFieldType;
    }

    public DtDmEnum getDtDmEnum() {
        return dtDmEnum;
    }

    public DtMysqlEnum getDtMysqlEnum() {
        return dtMysqlEnum;
    }

    public DtOracleEnum getDtOracleEnum() {
        return dtOracleEnum;
    }

    public DtKingbaseEnum getDtKingbaseEnum() {
        return dtKingbaseEnum;
    }

    public DtSqlserverEnum getDtSqlserverEnum() {
        return dtSqlserverEnum;
    }

    public DtPostgreEnum getDtPostgreEnum() {
        return dtPostgreEnum;
    }
}
